import { Transform, Type } from 'class-transformer';
import {
  IsDefined,
  IsNotEmptyObject,
  Min,
  ValidateNested,
} from 'class-validator';

export type SortOrder = 'descend' | 'ascend' | null;

export class ParamsDto {
  @IsDefined({ message: 'pageSize参数必填' })
  @Min(1, { message: '每页最少1条数据' })
  pageSize: number;

  @IsDefined({ message: 'current参数必填' })
  @Min(1, { message: '页码从1开始' })
  current: number;

  keyword?: string;

  dateTimeRange?: string[];
}

export class BaseQueryDto<T> {
  @IsDefined({ message: '查询参数必填' })
  @IsNotEmptyObject({ nullable: false }, { message: '查询参数必填' })
  @ValidateNested()
  @Type(() => ParamsDto)
  params: T & ParamsDto;

  sort?: Record<string, SortOrder>;
}

export class BaseFindDto<TWhere, TSelect> {
  @IsDefined({ message: '条件参数必填' })
  @IsNotEmptyObject({ nullable: false }, { message: '条件参数必填' })
  where: TWhere;

  select?: TSelect;
}

export class BaseDto<T> {
  @IsDefined({ message: '参数必填' })
  @IsNotEmptyObject({ nullable: false }, { message: '参数必填' })
  data: T;
}

export class WhiteListItem {
  @IsDefined({ message: '空投目标必填' })
  mobile: string;

  @IsDefined({ message: '空投数量必填' })
  @Transform(({ value }) => Number(value))
  @Min(1, { message: '空投数量必须大于0' })
  num: number;
}
